package com.lipiao.food.fragments;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.lipiao.food.R;
import com.lipiao.food.SectionsPagerAdapter;
import com.lipiao.food.foodFragments.HotS;
import com.lipiao.food.foodFragments.FoodGodS;
import com.lipiao.food.foodFragments.SeasonS;
import com.lipiao.food.foodFragments.StarS;
import com.lipiao.food.foodFragments.WeekS;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.WrapPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FoodFragment extends Fragment {

    View rootView;
    private static final String[] CHANNELS = new String[]{"一周热门", "当季最热", "人气菜肴", "明星菜谱", "食神菜谱"};
    private List<String> mDataList = Arrays.asList(CHANNELS);
    private MagicIndicator magicIndicator;
    private List<Fragment> fragments2;
    ViewPager viewPager2;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.food_fragment, container, false);
        initView();
        initListener();
        return rootView;
    }

    private void initView() {
        viewPager2 = rootView.findViewById(R.id.view_pager_food_fragment);
        fragments2 = new ArrayList<Fragment>();
        fragments2.add(new WeekS());
        fragments2.add(new SeasonS());
        fragments2.add(new HotS());
        fragments2.add(new StarS());
        fragments2.add(new FoodGodS());

        viewPager2.setAdapter(new SectionsPagerAdapter(getActivity().getSupportFragmentManager(), fragments2));

        viewPager2.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                viewPager2.setCurrentItem(i);//显示对应fragment
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        viewPager2.setCurrentItem(0);

        initMagicIndicator();
    }

    private void initListener() {

    }


    private void initMagicIndicator() {
        magicIndicator = rootView.findViewById(R.id.magic_indicator);
        magicIndicator.setBackgroundColor(Color.WHITE);
        CommonNavigator commonNavigator = new CommonNavigator(getContext());
        commonNavigator.setScrollPivotX(0.35f);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList == null ? 0 : mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context);
                simplePagerTitleView.setText(mDataList.get(index));
                simplePagerTitleView.setNormalColor(Color.parseColor("#333333"));
                simplePagerTitleView.setSelectedColor(Color.parseColor("#e94220"));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager2.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                WrapPagerIndicator indicator = new WrapPagerIndicator(context);
                indicator.setFillColor(Color.parseColor("#ebe4e3"));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPager2);
    }


}
